<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ProjectIndex extends MY_Controller {
	public function __construct(){
		parent::__construct();
		if(!$this->isLogin()){
			header('location:/Login/login');exit;
		}
		$this->load->model('Customer_model');
		$this->load->model('User_model');
		$this->load->model('Project_model');
		$this->load->model('ProjectRecord_model');
	}
	public function lists(){
		$where=array();
		if(isset($_POST['name'])&&$_POST['name']!=''){
			$name=$_POST['name'];
			$where['name']=array('like',trim($name));
		}
		$res=$this->Project_model->ajax_lists($where);
		foreach($res['list'] as $key=>$value){
			$customerwhere=array();
			$customerwhere['A.id']=array('=',$value['custmoerid']);
			$customeruser=$this->Customer_model->one($customerwhere);
			$value['customeruser']=$customeruser['name'];
			if($value['saleid']!=''){
				$salewhere=array();
				$salewhere['id']=array('=',$value['saleid']);
				$saleuser=$this->Customer_model->user_one($salewhere);
				$value['saleuser']=$saleuser['displayname'];
			}else{
				$value['saleuser']="无";
			}
			if($value['technologyuid']!=''){
				$technologywhere=array();
				$technologywhere['id']=array('=',$value['technologyuid']);
				$technologyuser=$this->Customer_model->user_one($technologywhere);
				$value['technologyuser']=$technologyuser['displayname'];
			}else{
				$value['technologyuser']="无";
			}
			$addwhere=array();
			$addwhere['id']=array('=',$value['adduid']);
			$adduser=$this->Customer_model->user_one($addwhere);
			$value['adduser']=$adduser['name'];
			if($value['addtime']!=''){
				$value['addtime']=date('Y-m-d H:i:s',$value['addtime']);
			}
			if($value['status']==0){
				$value['statusshwo']="开发中";
			}else if($value['status']==1){
				$value['statusshwo']="已上线";
			}
			$res['list'][$key]=$value;
		}
		if(isset($_POST['ajaxs'])){//ajax 获取分页数据
			$this->assign('list',$res['list']);
			$res['list']=$this->fetch('project/ajaxs_lists.html');
			ajax_return(1,'',$res);
		}else{
			$this->assign('list',$res["list"]);
			$this->assign('pageshow',$res['pageshow']);
			$this->display('project/lists.html');
		}
	}
	public function add(){
		if($_POST){
			if(!$_POST['name']||$_POST['name']==''){
				ajax_return(false,'名称不能为空');
			}
			if(!$_POST['custmoerid']||$_POST['custmoerid']==''){
				ajax_return(false,'请选择客服');
			}
			$_POST['addtime']=time();
			$_POST['adduid']=$this->pmsuserinfo['id'];
			unset($_POST['ajaxs']);
			//根据客服获取销售信息
			$xslist=$this->Project_model->getPmCustmoerXsUserInfo($_POST['custmoerid'],'id,saleuid');
			$data=$_POST;
			$data['saleid']=$xslist['saleuid'];
			$alist=$this->Project_model->add($data);
			if($alist){
				$keywords=$_POST['name']."添加成功";
				useraddrecord('项目添加',$keywords);
				ajax_return(true,'成功');
			}else{
				$keywords=$_POST['name']."添加失败";
				useraddrecord('项目添加',$keywords);
				ajax_return(false,'失败');
			}
		}else{
			$technologyuserwhere=array();
			$technologyuserwhere["type"]=array("=","2");
			$technologyuserlist=$this->Customer_model->user_all($technologyuserwhere);
			$this->assign('technologyuserlist',$technologyuserlist);
			$customerwhere=array();
			$customerlist=$this->Customer_model->all($customerwhere);
			$this->assign('customerlist',$customerlist);
			$this->display('project/add.html');
		}
	}
	public function edit(){
		if($_POST){
			$where=array();
			$where["id"]=array("=",$_POST['id']);
			$list=$this->Project_model->one($where);
			if(!$_POST['name']||$_POST['name']==''){
				ajax_return(false,'名称不能为空');
			}
			if(!$_POST['custmoerid']||$_POST['custmoerid']==''){
				ajax_return(false,'请选择客服');
			}
			unset($_POST['ajaxs']);
			//根据客服获取销售信息
			$xslist=$this->Project_model->getPmCustmoerXsUserInfo($_POST['custmoerid'],'id,saleuid');
			$data=$_POST;
			$data['saleid']=$xslist['saleuid'];
			
			$where=array();
			$where["id"]=array("=",$_POST['id']);
			$alist=$this->Project_model->edit($where,$data);
			if($alist){
				//判断客服是否修改；修改客服要修改项目记录 客户值
				if($list['custmoerid']!=$_POST['custmoerid']){
					$this->Project_model->setPmProjectRecordCustomerId($list['id'],$data['custmoerid']);
				}
				$keywords="项目id:".$list['id'].",".$_POST['name']."编辑成功";
				useraddrecord('项目编辑',$keywords);
				ajax_return(true,'成功');
			}else{
				$keywords="项目id:".$list['id'].",".$_POST['name']."编辑失败";
				useraddrecord('项目编辑',$keywords);
				ajax_return(false,'失败');
			}
		}else{
			if(isset($_GET['id'])){
				$where=array();
				$where["id"]=array("=",$_GET['id']);
				$list=$this->Project_model->one($where);
				if($list){
					$this->assign('list',$list);
					$technologyuserwhere=array();
					$technologyuserwhere["type"]=array("=","2");
					$technologyuserlist=$this->Customer_model->user_all($technologyuserwhere);
					$this->assign('technologyuserlist',$technologyuserlist);
					$customerwhere=array();
					$customerlist=$this->Customer_model->all($customerwhere);
					$this->assign('customerlist',$customerlist);
					$this->display('project/edit.html');
				}else{
					print("数据错误");
				}
			}else{
				print("数据错误");
			}
		}
	}
	public function implementation(){
		if($_POST['ajaxs']){
			$where=array();
			$where["id"]=array("=",$_POST['id']);
			$list=$this->Project_model->one($where);
			if(!$list){
				ajax_return(false,'数据错误');
			}
			$where=array();
			$where["id"]=array("=",$_POST['id']);
			$edit['status']=$_POST['status'];
			$alist=$this->Project_model->edit($where,$edit);
			if($alist){
				ajax_return(true,'成功');
				// $recordwhere=array();
				// $recordwhere["prjid"]=array("=",$list['id']);
				// $recordlist=$this->Project_model->recordone($recordwhere);
				// if($recordlist){
					// $recordedit['prjstaus']=$_POST['status'];
					// $recordeditlist=$this->Project_model->recordedit($recordwhere,$recordedit);
					// if($recordeditlist){
						// ajax_return(true,'成功');
					// }else{
						// ajax_return(true,'修改数据成功，修改记录失败');
					// }
				// }else{
					// ajax_return(true,'成功');
				// }
			}else{
				ajax_return(false,'失败');
			}
		}else{
			ajax_return(false,'数据错误');
		}
	}
	public function del(){
		if($_POST['ajaxs']){
			$where=array();
			$where['id']=array('=' , $_POST['id']);
			$list=$this->Project_model->one($where);
			if(!$list){
				ajax_return(false,'数据错误');
			}
			$where=array();
			$where['id']=array('=' , $_POST['id']);
			$dellist=$this->Project_model->del($where);
			if($dellist){
				ajax_return(true,'删除成功');	
			}else{
				ajax_return(false,'删除失败');
			}
		}			
	}
}
